package com.whq.common.mail;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * 
 * 发送邮件测试
 * 
 * @author wanghuaiqiang 2010-05-11
 *
 */
public class TestSendMail {
	public static void main(String args[]) throws Exception {
		MailModel mailModel = null;
		//发送简单的文本邮件
		mailModel = new MailModel();
		mailModel.setAuthPassword("111111");
		mailModel.setAuthUsername("stout_jiang");
		mailModel.setContent("测试");
		mailModel.setFrom("stout_jiang@163.com");
		mailModel.setSmtpServer("smtp.163.com");
		mailModel.setSubject("邮件测试");
		String[] toMailList1 = { "jiangwanchun@im-sh.com" };
		mailModel.setToMailList(toMailList1);
		SendMail.send(mailModel);
		System.out.println("邮件发送成功！");
		
		//通过SSL发送简单的文本邮件
		mailModel = new MailModel();
		mailModel.setAuthPassword("50551676");
		mailModel.setAuthUsername("send_service@whq.com");
		mailModel.setMailType(MailModel.MAIL_TYPE_HTML);
		String mailContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
				"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" +
				"<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">" +
				"<head>" +
				"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"> " +
				"<title>测试111</title>" +
				"</head>" +
				"<body>" +
				"<font color='red'>测试1111</font>" +
				"</body> " +
				"</html>";
		mailModel.setContent(mailContent);
		mailModel.setContentType("text/html;charset=utf-8");
		mailModel.setFrom("send_service@whq.com");
		mailModel.setFromName("乐趣网");
		mailModel.setSmtpServer("smtp.gmail.com");
		mailModel.setSmtpPort(465);
		mailModel.setSubject("test mail");
		String[] toMailList2 = { "iastar@21cn.com" };
		mailModel.setToMailList(toMailList2);
		SendMail.sendViaSsl(mailModel);
		System.out.println("邮件发送成功！");
		
		//发送带附件的邮件
		mailModel = new MailModel();
		mailModel.setAuthPassword("111111");
		mailModel.setAuthUsername("stout_jiang");
		String content = TestSendMail.getHTMLContent(2);
		mailModel.setContent(content);
		mailModel.setFrom("stout_jiang@163.com");
		mailModel.setSmtpServer("smtp.163.com");
		mailModel.setSubject("邮件测试");
		String[] toMailList3 = { "jiangwanchun@im-sh.com" };
		mailModel.setToMailList(toMailList3);

		File file = new File("D:/barchart100.png");
		byte[] imgByte = TestSendMail.getImageBytes(file);
		mailModel.getAttachList().add(imgByte);
		mailModel.getAttachList().add(imgByte);

		SendMail.send(mailModel);
		System.out.println("邮件发送成功！");
	}

	private static String getHTMLContent(int size) {
		StringBuffer sb = new StringBuffer();
		sb.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"");
		sb.append("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
		sb
				.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">");
		sb.append("<head><title>MT4短信中心气象通业务量</title></head>");
		sb.append("<body><p>");
		for (int i = 0; i < size; i++) {
			sb.append("<img src=\"cid:IMG" + i
					+ "\" width=\"600\" height=\"400\" border=\"0\"/>");
		}
		// sb.append("<img src=\"cid:IMG0\" width=\"600\" height=\"400\" border=\"0\"/>");
		sb.append("</p></body>");
		sb.append("</html>");
	
		String content = sb.toString();
		return content;
	}

	private static byte[] getImageBytes(File file) throws FileNotFoundException {
		byte[] myData = null;
		InputStream input = new FileInputStream(file);
		try {
			ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
			int ch = 0;
			while ((ch = input.read()) != -1) {
				byteArray.write(ch);
			}
			// System.out.println(byteArray.size());
			myData = byteArray.toByteArray();
			// System.out.println(myData.length);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return myData;
	}
}
